﻿@model YiYiTong.Rocket.Model.Manager.UserModel

@{
    ViewBag.Title = "用户设置角色";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<form method="post" class="definewidth m20">
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td colspan="2">
                <h5>用户设置角色，标<em>*</em>号是必填或必选项。</h5>
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">用户编号</td>
            <td><input type="text" id="UID" name="UID" readonly="readonly" value="@Model.uid" /></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">用户名</td>
            <td><input type="text" name="LoginName" readonly="readonly" value="@Model.loginName" /></td>
        </tr>
        <tr>
            <td class="tableleft">角色<em>*</em></td>
            <td>
                @Html.Action("CheckBoxRoleList", "Role", new { EleAttr = new ElementAttribute { Class = "sce-wid02", Name = "RoleID" } })
            </td>
        </tr>
        <tr>
            <td class="tableleft"></td>
            <td>
                <p><span id="spMsg"></span></p>
                <button type="button" class="btn btn-primary" id="save">保存</button> &nbsp;&nbsp;
                <button type="button" class="btn btn-success" name="backid" id="backid">返回</button>
            </td>
        </tr>
    </table>
    <script>
        BUI.use('bui/overlay', function (overlay) {
            function showSucc(obj, url) {
                BUI.Message.Alert(obj, function () {
                    location.href = url;
                }, 'success');
            }
            $(function () {
                $('#backid').on('click', function () {
                    window.document.location.href = "@Url.Action("UserList", "User")";
                });

                //绑定用户原有设置角色
                var ids = '@Model.RoleIds'.split(",");
                $("input:checkbox").each(function () {
                    var role = $(this);
                    var roleValue = $(this).val();

                    if (ids != "") {
                        $.each(ids, function () {
                            if (this == roleValue)
                                role.attr("checked", "checked");
                        });
                    }
                });

                //保存
                $('#save').on('click', function () {
                    var uid = '@Model.uid';
                    var roleIds = "";
                    var isSelect = false;
                    //遍历角色checkbox
                    $("input:checkbox").each(function () {
                        if ($(this).attr("checked") == "checked") {
                            roleIds += $(this).val() + ',';
                            isSelect = true;
                        }
                    });

                    if (!isSelect) {
                        $("#spMsg").html('请选择角色');
                        return;
                    }

                    $.post('@Url.Action("SetRole","User")', { UID: uid, RoleIds: roleIds }, function (result) {
                        if (result.Message != undefined) {
                            if (result.IsSuccess) {
                                showSucc(result.Message, result.Body);
                            }
                            else {
                                $("#spMsg").html(result.Message);
                            }
                        }
                        else {
                            var substr = cutstr(result, "BUI.Message.Alert", "function");
                            substr = substr.replace("'", "").replace("'", "").replace("(", "").replace(",", "");
                            if (substr == "")
                                BUI.Message.Alert("操作失败，请联系技术人员", 'warning');
                            else {
                                if (substr.indexOf("请重新登录") > 0) {
                                    BUI.Message.Alert(substr, function () {
                                        top.location.href = "@Url.Action("Login","User")";
                                    }, 'warning');
                                }
                                else {
                                    BUI.Message.Alert(substr, 'warning');
                                }
                            }
                        }
                    });
                });
            });
        });
    </script>
</form>
